__author__ = 'deadmoroz'

class Language(object):
    def __init__(self, map):
        default = self.__default()
        self.__dict__.update(default)
        for field in map:
            if field in default:
                self.__dict__[field] = map[field]

    def __default(self):
       return {
            "caseInsensitive": False,
            "defaultMode": None,
            "name": None
       }


class Mode(object):
    def __init__(self, dict):
        default = self.__default()
        self.__dict__.update(default)
        for field in dict:
            if field in default:
                self.__dict__[field] = dict[field]

    def __default(self):
        return {
            "compiled" : False,
            "begin" : "",
            "end" : None,
            "endsWithParent" : False,
            "illegal" : None,
            "contains" : [],
            "keywords" : {},
            "starts": None,
            "lexems" : [],
            "terminators": [],
            "subLanguage": None,
            "lexemsRe": None,
            "className": None,
            "returnBegin": False,
            "excludeBegin": False,
            "excludeEnd": False,
            "returnEnd": False
        }




class ModeNotFound(Exception):
    pass

class Registry(object):
    _BACKSLASH_ESCAPE = Mode({"begin": "\\\\.", "relevance": 0})
    _NUMBER_RE = "\\b\\d+(\\.\\d+)?"
    _modes = {
        "BACKSLASH_ESCAPE" : _BACKSLASH_ESCAPE,
        "APOS_STRING_MODE" : Mode({
            "className": "string",
            "begin": "\\'", "end": "\\'",
            "illegal": "\\n",
            "contains": [_BACKSLASH_ESCAPE],
            "relevance": 0
        }),
        "QUOTE_STRING_MODE" : Mode({
            "className": "string",
            "begin": "\"",
            "end": "\"",
            "illegal": "\\n",
            "contains": [_BACKSLASH_ESCAPE],
            "relevance": 0
        }),

        "NUMBER_MODE" : Mode({
            "className": "number",
            "begin": _NUMBER_RE,
            "relevance": 0
        })
    }


    def __getitem__(self, item):
        if item in Registry._modes:
            return Registry._modes[item]
        raise ModeNotFound()
